package cn.bugstack.trigger.listener;

import cn.bugstack.domain.activity.service.ISkuStock;
import cn.bugstack.types.event.BaseEvent;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Title: ActivitySkuStockZeroCusomer
 * @Author lz
 * @Date 2025/8/29 16:29
 * @description:
 */
@Slf4j
@Component
public class ActivitySkuStockZeroCustomer {
    @Resource
    private ISkuStock skuStock;

    @RabbitListener(queuesToDeclare = @Queue(value = "${spring.rabbitmq.topic.activity_sku_stock_zero}"))
    public void listener(String  message){
        try {
            log.info("监听活动库存为零消息，处理开始 topic: {} message: {}", "activity_sku_stock_zero", message);
            //转换对象 todo
            BaseEvent.EventMessage<Long> eventMessage = JSON.parseObject(message, new TypeReference<BaseEvent.EventMessage<Long>>() {
            }.getType());
            Long sku = eventMessage.getData();
            // 处理活动库存为零
            skuStock.clearActivitySkuStock(sku);
            // 处理库存队列
            skuStock.clearQueueValue();
        } catch (Exception e) {
            log.error("监听活动库存为零消息，处理失败 ",e);
            throw e;
        }

    }
}
